package cn.neusoft.yuejian307.model;

import android.content.Context;
import android.widget.Toast;

import java.util.List;

import cn.neusoft.yuejian307.GloableVirable;
import cn.neusoft.yuejian307.bean.Choiceness;
import cn.neusoft.yuejian307.bean.Coach;
import cn.neusoft.yuejian307.bean.NewBean;
import cn.neusoft.yuejian307.bean.Train;
import cn.neusoft.yuejian307.bean.User;
import cn.neusoft.yuejian307.bean.Video;
import cn.neusoft.yuejian307.listener.UserListener;
import cn.neusoft.yuejian307.myinterface.UserIface;
import cn.neusoft.yuejian307.service.YuejianService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserModel implements UserIface {
    private YuejianService service;
    private Retrofit retrofit;
    public static boolean SEND_NEWS_FLAG = false;

    public UserModel() {
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(GloableVirable.URL)
                .build();

    }


    @Override
    public void registerData(final UserListener<User> listener,String phone,String username,String password) {//注册
        service= retrofit.create(YuejianService.class);//注册
        Call<User> call = service.register(phone,username,password);
        //Retrofit使用-3
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void loginData(final UserListener<User> listener,String phone,String password) {//登录
        service= retrofit.create(YuejianService.class);
        Call<User> call = service.userLogin(phone,password);
        //Retrofit使用-3
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void activityData(final UserListener<Choiceness> listener) {//活动
        service= retrofit.create(YuejianService.class);
        Call<Choiceness> call = service.activitylist();
        //Retrofit使用-3
        call.enqueue(new Callback<Choiceness>() {
            @Override
            public void onResponse(Call<Choiceness> call, Response<Choiceness> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<Choiceness> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void applyData(final UserListener<User> listener, int user_id, int activity_id, String activity_name, String activity_type, String activity_price, String user_phone, String user_name, String paynum) {
        service= retrofit.create(YuejianService.class);//陪培训报名
        Call<User> call = service.activityApply( user_id,  activity_id,
         activity_name,  activity_type,  activity_price,  user_phone,
                 user_name,  paynum);
        //Retrofit使用-3
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void coachData(final UserListener<Coach> listener, int club_id) {//大师
        service= retrofit.create(YuejianService.class);
        Call<Coach> call = service.coachlist(club_id);
        //Retrofit使用-3
        call.enqueue(new Callback<Coach>() {
            @Override
            public void onResponse(Call<Coach> call, Response<Coach> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<Coach> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void videoData(final UserListener<Video> listener, int club_id) {//视频
        service= retrofit.create(YuejianService.class);
        Call<Video> call = service.clubVideo(1);
        //Retrofit使用-3
        call.enqueue(new Callback<Video>() {
            @Override
            public void onResponse(Call<Video> call, Response<Video> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<Video> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void trainData(final UserListener<List<Train>> listener, int flag) {//培训
        service= retrofit.create(YuejianService.class);
        Call<List<Train>> call = service.trainlist(flag);
        //Retrofit使用-3
        call.enqueue(new Callback<List<Train>>() {
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<List<Train>> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void trianApplyData(final UserListener<User> listener, int userid, int trainid, String name, String price, String phone) {
        service= retrofit.create(YuejianService.class); //活动报名
        Call<User> call = service.trainApply(userid,trainid,name,price,phone);
        //Retrofit使用-3
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void newsData(final UserListener<List<NewBean>> listener, int club_id) {
        service= retrofit.create(YuejianService.class);//大师问答列表
        Call<List<NewBean>> call = service.newslist(club_id);
        //Retrofit使用-3
        call.enqueue(new Callback<List<NewBean>>() {
            @Override
            public void onResponse(Call<List<NewBean>> call, Response<List<NewBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<List<NewBean>> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void addNewsData( int club_id, int send_id, String content, final Context context) {
        service= retrofit.create(YuejianService.class);//大师问答发消息
        Call<User> call = service.addnews(club_id,send_id,content);
        //Retrofit使用-3
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful()&&response.body().getSuccess()==1){
                    //调用监听事件接口的onResponse方法
                    Toast.makeText(context,"发送成功",Toast.LENGTH_LONG).show();
                    SEND_NEWS_FLAG  = true;
                }
                else{
                    Toast.makeText(context,"发送失败",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                //调用监听事件接口的onFail方法
                Toast.makeText(context,"网络错误",Toast.LENGTH_LONG).show();
            }
        });
    }


}